<!--
  移动签批->公文详情->发电->修改公文要素
-->
<template>
  <view>
    <van-popup v-model="showCompo" closeable round position="bottom" :style="{ height: '100%' }"
               @click-close-icon="toClose" @click-overlay="toClose">
      <van-row type="flex" justify="center">
        <van-col offset="1" span="8">
          <van-cell value="修改公文要素"/>
        </van-col>
      </van-row>
      <van-form>
        <van-collapse v-model="tableShow" accordion :border="border">
          <van-collapse-item icon="label-o" title="公文要素" title-class="text-bold" name="jbxx">
            <van-field ref="title" v-model="formData.title" required label="标题" placeholder="请输入" input-align="right" autosize rows="1" type="textarea" />

            <van-cell is-link title="密级" :value="this.dict.getDictNameByDictCode('mj',formData.secrecyLevel)"
                      @click="show = true"/>
            <van-action-sheet v-model="show" :actions="mj" cancel-text="取消" @select="onSelect"/>

            <van-cell is-link title="缓急" required :value="this.dict.getDictNameByDictCode('gwfdjjcd',formData.emergencyLevel)"
                      @click="show1 = true"/>
            <van-action-sheet v-model="show1" :actions="hj" cancel-text="取消" @select="onSelect1"/>

            <van-field label="拟稿日期" :value="formData.createTime" @click="showCalendar = true" placeholder="请选择"
                       input-align="right" is-link/>
            <van-calendar v-model:showCalendar="showCalendar" :show-confirm="false" @confirm="onConfirm"/>

            <van-field v-model="formData.eachPage" label="页数" placeholder="请输入" input-align="right"/>

            <van-cell title="落款单位" required :value="formData.signDeptName" is-link @click="showOrgTreeSheet('1')"/>
            <orgTree :showOrgtree="showOrgtreeLK" :nodeData="nodeDataDrafter" :drafterDeptData="drafterSignDeptData"
                     @closeOrgTreeSheet="closeOrgTreeSheet"></orgTree>

            <van-cell ref="drafterDeptName" required title="承办单位" :value="formData.drafterDeptName" is-link
                      @click="showOrgTreeSheet('2')"/>
            <orgTree2 :showOrgtree="showOrgtreeCB" :nodeData="nodeDataDrafter" :drafterDeptData="drafterDeptData"
                      @closeOrgTreeSheet="closeOrgTreeSheet"></orgTree2>

            <van-cell ref="name" required title="拟稿" :value="formData.drafterName" @click="showDrafterSheet" is-link/>
            <drafter :showDrafter="showDrafter" :nodeData="nodeDataDrafter" :drafterData="drafterData"
                     @closeDrafterSheet="closeDrafterSheet"></drafter>

            <van-field ref="phone" required v-model="formData.drafterPhone" required label="联系电话" type="tel" placeholder="请输入"
                       input-align="right"/>

            <van-cell ref="public" required is-link title="是否公开"
                      :value="this.dict.getDictNameByDictCode('infopublic',formData.infoDisclosure)"
                      @click="show3 = true"/>
            <van-action-sheet v-model="show3" :actions="sfgk" cancel-text="取消" @select="onSelect3"/>

            <van-field v-if="formData.infoDisclosure==1" v-model="formData.nopublicReason" label="不公开理由"
                       placeholder="请输入" input-align="right"/>

            <van-cell is-link title="文件属性"
                      :value="this.dict.getDictNameByDictCode('fileAttribute',formData.fileAttribute)"
                      @click="show4 = true"/>
            <van-action-sheet v-model="show4" :actions="wjsx" cancel-text="取消" @select="onSelect4"/>

            <van-cell is-link title="是否为重要政策性文件" :value="this.dict.getDictNameByDictCode('isPolicy',formData.isPolicy)"
                      @click="show5 = true"/>
            <van-action-sheet v-model="show5" :actions="sfwzyzcwj" cancel-text="取消" @select="onSelect5"/>

            <van-cell is-link title="是否有解决方案"
                      :value="this.dict.getDictNameByDictCode('isInterpret',formData.isInterpret)"
                      @click="show6 = true"/>
            <van-action-sheet v-model="show6" :actions="sfyjjfa" cancel-text="取消" @select="onSelect6"/>

            <van-cell is-link title="是否有文件合法性审核结果" :value="this.dict.getDictNameByDictCode('isLegal',formData.isLegal)"
                      @click="show7 = true"/>
            <van-action-sheet v-model="show7" :actions="sfwywjhfxshhjg" cancel-text="取消" @select="onSelect7"/>

            <van-cell ref="storagePeriod" is-link title="保管期限" required
                      :value="this.dict.getDictNameByDictCode('bgqx',formData.storagePeriod)" @click="show8 = true"/>
            <van-action-sheet v-model="show8" :actions="bgqx" cancel-text="取消" @select="onSelect8"/>

            <van-cell title="会签单位" :value="formData.countersign" is-link @click="showSignOrgSheet()"/>
            <signOrg :showSignOrg="showSignOrg" :checkedVal="formData.countersign" :fwId="formData.extendIdForRelate"
                     @closeSignOrgSheet="closeSignOrgSheet"></signOrg>

            <van-field v-model="formData.description" label="拟稿说明" placeholder="请输入" input-align="right"/>

          </van-collapse-item>
          <van-collapse-item icon="font-o" title="已附加公文" title-class="text-bold" name="zwjfj">
            <view class="opinion" v-if="relation.length>0 ">
              <text class="opinion-left">附加公文</text>
              <view class="opinion-right">
                <text v-for="(item,index) in relation">{{ item.title }}</text>
              </view>
            </view>
          </van-collapse-item>
        </van-collapse>
      </van-form>

      <view class="padding-top-lg padding-bottom-lg">
        <van-row type="flex" justify="space-around">
          <van-col span="10">
            <van-button type="info" round plain hairline block @click="toClose">取消</van-button>
          </van-col>
          <van-col span="10">
            <van-button type="info" round block @click="toEdit">确定</van-button>
          </van-col>
        </van-row>
      </view>
    </van-popup>
  </view>
</template>

<script>
import forward from 'pages/caac/components/sendMsgDoc/compo-forward';
import orgTree from 'pages/caac/components/sendMsgDoc/compo-orgtree';//单位选择组件
import orgTree2 from 'pages/caac/components/sendMsgDoc/compo-orgtree';//单位选择组件
import signOrg from 'pages/caac/components/sendMsgDoc/compo-signorg';//会签单位选择组件
import drafter from 'pages/caac/components/common/compo-drafter';//拟稿人选择
export default {
  components: {
    forward,
    orgTree,
    orgTree2,
    signOrg,
    drafter
  },
  props: {
    showEdit: {
      type: Boolean,
      default: false
    },
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
    showEdit(val) {
      this.showCompo = val
    },

  },
  data() {
    return {
      showCompo: false,//组件显示
      showCalendar: false,//日历组件显示
      //1主送 3抄送 4分送
      forwardState1: false,//分送
      forwardState3: false,//抄送
      forwardState4: false,//分送
      showOrgtreeLK: false,//落款单位选择
      showOrgtreeCB: false,//承办单位选择
      showSignOrg: false,//会签单位选择
      showDrafter: false,//拟稿人组件
      //拟稿人选择，查询单位所需节点和隐藏节点
      nodeDataDrafter: {
        showNode: '', //显示的节点
        hideNode: '', //隐藏的节点
      },
      //拟稿人信息，串回子组件回显用
      drafterData: {
        userName: '',
        userId: ''
      },
      //承办单位信息回显用
      drafterDeptData: {
        drafterDeptId: '',
        drafterDeptName: ''
      },
      //落款单位
      drafterSignDeptData: {
        drafterDeptId: '',
        drafterDeptName: ''
      },
      countersign: [],//会签单位名称数组
      countersignValue: [],//会签单位值数组
      relation: [],//附加公文列表
      filterText: '',
      expandKeys: ["0"],//默认展开的节点-对应的deptId的值
      props: function () {
        return {
          label: 'deptName', // 指把数据中的‘personName’当做label也就是节点名称
          children: 'children' // 指把数据中的‘childs’当做children当做子节点数据
        }
      },
      ready: false, // 这里用于自主控制loading加载状态，避免异步正在加载数据的空档显示“暂无数据”
      treeData: [],
      showOrg: false,
      formData: {},//表单数据
      showPopup: false,
      border: false,
      show: false,
      show1: false,
      show2: false,
      show3: false,
      show4: false,
      show5: false,
      show6: false,
      show7: false,
      show8: false,
      topTitle: 'gwxq',
      tableShow: "jbxx",
      bjsj: '',
      password: '',
      to: '',
      cc: '',
      finishTime: '',
      bt: '',
      zs: '',
      zpsfys: '',
      bgkly: '',
      xzhqdw: '',
      ngsm: '',
      ng: '',
      lxdh: '',
      zwjfj: '',
      xgcl: '',
      gw1: '',
      gw2: '',
      mj: [{
        name: '无'
      }, {
        name: '普通加密'
      }, {
        name: '特殊加密'
      }],
      hj: [{
        name: '平件1'
      }, {
        name: '急件1'
      }, {
        name: '特急1'
      }],
      xzxklgw: [{
        name: '是'
      }, {
        name: '否'
      }],
      sfgk: [{
        name: '不公开'
      }, {
        name: '主动公开'
      }, {
        name: '依申请公开'
      }],
      wjsx: [{
        name: '非行政规范性文件'
      }, {
        name: '一般行政规范性文件'
      }, {
        name: '重要行政规范性文件'
      }],
      sfwzyzcwj: [{
        name: '是'
      }, {
        name: '否'
      }],
      sfyjjfa: [{
        name: '是'
      }, {
        name: '否'
      }],
      sfwywjhfxshhjg: [{
        name: '是'
      }, {
        name: '否'
      }],
      bgqx: [],//保管期限
      sfdcfkgw: [{
        name: '是'
      }, {
        name: '否'
      }],
      sfsdcsx: [{
        name: '是'
      }, {
        name: '否'
      }],
      mjT: "无",
      hjT: "平件",
      xzxklgwT: "否",
      sfgkT: "不公开",
      wjsxT: "请选择",
      sfwzyzcwjT: "否",
      sfyjjfaT: "否",
      sfwywjhfxshhjgT: "否",
      sfsdcsxT: "否",
      title: '',
      author: '',
      digit: '',
      message: '',
      countersignDept: '',
      countersignDeptId: '',
      reason: '',
    };
  },
  mounted() {
    //从缓存加载单位树
    this.getOrgTree()
    this.getDict()//获取字典项
    this.getShowDept()//获取要显示的组织机构
  },

  methods: {
    //拟稿人
    showDrafterSheet() {
      this.showDrafter = true
    },
    //关闭拟稿人组件
    closeDrafterSheet(userInfo) {
      this.formData.drafterId = userInfo.userId
      this.formData.drafterName = userInfo.userName
      this.showDrafter = false
    },
    onConfirm(date) {
      const formatDate = (date) => `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
      this.showCalendar = false
      this.formData.createTime = formatDate(date)
    },
    //类型 1主送 3抄送 4分送
    showforward(tagType) {
      if (tagType == 1) {
        this.forwardState1 = true
      } else if (tagType == 3) {
        this.forwardState3 = true
      } else if (tagType == 4) {
        this.forwardState4 = true
      }

    },

    //子组件通知父组件关闭
    closeSheet(data) {
      //1主送 3抄送 4分送
      if (data.tagType == 1) {
        if (data.tagId != '') {
          this.formData.mainTo = data.content
        }
        this.forwardState1 = false
      } else if (data.tagType == 3) {
        if (data.tagId != '') {
          this.formData.copyTo = data.content
        }
        this.forwardState3 = false
      } else if (data.tagType == 4) {
        if (data.tagId != '') {
          this.formData.copyReports = data.content
        }
        this.forwardState4 = false
      }

    },

    //点击会签单位
    showSignOrgSheet() {
      this.showSignOrg = true
    },
    //落款单位选择type=1,落款单位选择，type=2，承办单位选择
    showOrgTreeSheet(type) {
      if (type == 1) {
        this.showOrgtreeLK = true
      } else if (type == 2) {
        this.showOrgtreeCB = true
      } else {
        console.log('页面出错了');
      }

    },
    //子组件通知父组件关闭单位选择
    closeOrgTreeSheet(data) {
      if (this.showOrgtreeCB) {
        this.showOrgtreeCB = false
        this.formData.drafterDeptName = data.orgName//承办单位名称
        this.formData.drafterDeptId = data.deptCode//承办单位ID
      }
      if (this.showOrgtreeLK) {
        this.showOrgtreeLK = false
        this.formData.signDeptName = data.orgName//落款单位名称
        this.formData.signDept = data.deptCode//落款单位ID
      }

    },
    //子组件通知父组件关闭机构树
    closeSignOrgSheet(data) {
      this.countersign = []//置空，以防多次追加
      this.formData.countersign = data.toString()//给表单赋值，提交表单用
      this.showSignOrg = false
    },
    //密级
    onSelect(item) {
      this.formData.secrecyLevel = item.dictItemCode
      this.show = false;
    },
    //缓急
    onSelect1(item) {
      this.formData.emergencyLevel = item.dictItemCode
      this.show1 = false;
    },
    //行政许可类公文
    onSelect2(item) {
      this.formData.xzsplgw = item.dictItemCode
      this.show2 = false;
    },
    //是否公开
    onSelect3(item) {
      this.formData.infoDisclosure = item.dictItemCode
      this.show3 = false;
    },
    //文件属性
    onSelect4(item) {
      this.formData.fileAttribute = item.dictItemCode
      this.show4 = false;
    },
    //是否为重要政策性文件
    onSelect5(item) {
      //如果选择是，则是否有解读文件和是否有文件合法性审核结果都为选中
      if (item.name == '是') {
        this.formData.isInterpret = this.sfyjjfa.filter(v => v.name == '是')[0].dictItemCode
        this.formData.isLegal = this.sfwywjhfxshhjg.filter(v => v.name == '是')[0].dictItemCode
      }
      this.formData.isPolicy = item.dictItemCode
      this.show5 = false;
    },
    //是否有解读方案
    onSelect6(item) {
      this.formData.isInterpret = item.dictItemCode
      this.show6 = false;
    },
    //是否有文件合法性审查结果
    onSelect7(item) {
      this.formData.isLegal = item.dictItemCode
      this.show7 = false;
    },
    onSelect8(item) {
      this.formData.storagePeriod = item.dictItemCode
      this.show8 = false;
    },
    //取消按钮
    toClose() {
      this.$emit('closeEdit', this.showEdit);
    },
    //确定按钮
    toEdit() {
      this.saveelement()
    },
    // 表单校验
    validateForm() {
      if (this.util.isEmpty(this.formData.title)) {
        this.$refs.title.focus()
        this.$toast('请填写标题')
        return false
      }
      if (this.util.isEmpty(this.formData.infoDisclosure)) {
        this.$refs.public.focus()
        this.$toast('请选择是否公开')
        return false
      }
      if (this.util.isEmpty(this.formData.drafterName)) {
        this.$refs.name.focus()
        this.$toast('请填写拟稿人')
        return false
      }
      if (this.util.isEmpty(this.formData.drafterPhone)) {
        this.$refs.phone.focus()
        this.$toast('请填写联系电话')
        return false
      }
      return true;
    },
    //修改公文要素
    saveelement() {
      var _this = this
      if (_this.validateForm()) {
        this.$httploading({
          // 修改呈批件接口和发文发电不一样
          url: this.$http.adornUrl(`/caac/oa/doc/fdj/saveelement?systemKey=` + this.cache.get('userInfo').systemKey + `&systemFlag=` + this.cache.get('userInfo').systemFlag),
          method: 'post',
          data: JSON.stringify(this.formData)
        }).then(({data}) => {
          if (data.code == 200) {
            _this.$toast('修改公文要素成功')
            _this.$emit('closeEdit', this.showEdit);
            _this.$emit('refreshBtnGroup', true)//修改成功后通知父组件重新加载数据
          }
        })
      }
    },
    //父组件点击修改公文要素时调用获取详情回显到表单
    editGw(proInstId) {
      console.log("获取公文要素详情" + proInstId);
      const _this = this
      this.$httploading({
        url: this.$http.adornUrl(`/caac/oa/doc/fdj/proInstId/` + proInstId),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag//固定值
        })
      }).then(({data}) => {
        if (data.code == 200) {
          _this.formData = data.data
          this.drafterData.userId = _this.formData.drafterId
          this.drafterData.userName = _this.formData.drafterName
          //承办单位
          this.drafterDeptData.drafterDeptId = _this.formData.drafterDeptId
          this.drafterDeptData.drafterDeptName = _this.formData.drafterDeptName

          //落款单位
          this.drafterSignDeptData.drafterDeptId = _this.formData.signDept
          this.drafterSignDeptData.drafterDeptName = _this.formData.signDeptName
          _this.getRelation(data.data.extendIdForRelate)
        }
      })
    },
    //获取附加公文
    getRelation(extendIdForRelate) {
      const _this = this
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/mhjComponent/relationfile/relation`),
        method: 'get',
        params: this.$http.adornParams({
          'docId': extendIdForRelate,
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag//固定值
        })
      }).then(({data}) => {
        if (data.code == 200) {
          if (data.data != null) {
            _this.relation = data.data
          }
        }
      })
    },
    //获取要显示的组织机构代码
    getShowDept() {
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/doc/fw/getShowDept`),
        method: 'get',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag//固定值
        })
      }).then(({data}) => {
        if (data.code == 200) {
          if (data.data != null) {
            this.nodeDataDrafter.showNode = data.data.hideDepts
            this.nodeDataDrafter.hideNode = ''//要隐藏的节点
          }
        }
      })
    },
    getOrgTree() {
      this.treeData = this.cache.get('orgTree')
    },
    //获取需要的字典项
    getDict() {
      const dictSecrecyLevel = this.cache.get('mj')//密级
      //给van-action-sheet 赋值
      this.mj = []
      dictSecrecyLevel.forEach((item) => {
        this.mj.push({name: item.dictItemName, dictItemCode: item.dictItemCode})
      })

      const dictEmergencyLevel = this.cache.get('gwfdjjcd')//紧急程度
      this.hj = []
      dictEmergencyLevel.forEach((item) => {
        this.hj.push({name: item.dictItemName, dictItemCode: item.dictItemCode})
      })

      const dictXzsplgw = this.cache.get('xzsplgw')//紧急程度
      this.xzxklgw = []
      dictXzsplgw.forEach((item) => {
        this.xzxklgw.push({name: item.dictItemName, dictItemCode: item.dictItemCode})
      })

      const dictInfoDisclosure = this.cache.get('infopublic')
      this.sfgk = []
      dictInfoDisclosure.forEach((item) => {
        this.sfgk.push({name: item.dictItemName, dictItemCode: item.dictItemCode})
      })

      const dictFileAttribute = this.cache.get('fileAttribute')//文件属性
      this.wjsx = []
      dictFileAttribute.forEach((item) => {
        this.wjsx.push({name: item.dictItemName, dictItemCode: item.dictItemCode})
      })

      const dictIsPolicy = this.cache.get('isPolicy')//是否为重要政策性文件
      this.sfwzyzcwj = []
      dictIsPolicy.forEach((item) => {
        this.sfwzyzcwj.push({name: item.dictItemName, dictItemCode: item.dictItemCode})
      })

      const dictIsInterpret = this.cache.get('isInterpret')//是否有解决方案
      this.sfyjjfa = []
      dictIsInterpret.forEach((item) => {
        this.sfyjjfa.push({name: item.dictItemName, dictItemCode: item.dictItemCode})
      })

      const dictIsLegal = this.cache.get('isLegal')//是否有文件合法性审查结果
      this.sfwywjhfxshhjg = []
      dictIsLegal.forEach((item) => {
        this.sfwywjhfxshhjg.push({name: item.dictItemName, dictItemCode: item.dictItemCode})
      })

      //保管期限
      const dictBgqx = this.cache.get('bgqx')//是否有文件合法性审查结果
      this.bgqx = []
      dictBgqx.forEach((item) => {
        this.bgqx.push({name: item.dictItemName, dictItemCode: item.dictItemCode})
      })

      const dictIsSupervise = this.cache.get('isSupervise')//是否有文件合法性审查结果
      this.sfdcfkgw = []
      dictIsSupervise.forEach((item) => {
        this.sfdcfkgw.push({name: item.dictItemName, dictItemCode: item.dictItemCode})
      })
    },
  },
};
</script>

<style scoped>
  /* 意见相关样式 */
  .opinion{
    display: flex;
    flex-direction: column;
    margin: 0 32rpx;
    padding: 20rpx 0rpx;
    overflow: hidden;
    font-size: 28rpx;
    line-height: 24px;
    border-bottom: 1px solid #ebedf0;

  }
  .opinion .opinion-left{
    text-align: left;
    color: #323233;
  }
  .opinion .opinion-right{
    text-align: right;
    color: #969799;
  }
	.van-cell__title {
		min-width: 55%;

		span {
			display: inline-block;
			text-align: left;
			word-break: break-all;
		}
	}

	/deep/ .van-collapse-item__content {
		padding: 0px;
		color: #969799;
		font-size: 14px;
		line-height: 1.5;
		background-color: #fff;
	}

	/deep/ .van-field__label {
		color: #323233;
		min-width: 50%;

		span {
			display: inline-block;
			text-align: left;
			word-break: break-all;
		}
	}

	/deep/ .van-field__control {
		font-size: 14px;
		color: #969799;
	}
</style>
